package com.coffee.order.service.sku.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.sku.query.UrbanFullReductionQuery;
import com.coffee.order.api.sku.result.UrbanFullReductionResult;
import com.coffee.order.api.sku.service.UrbanFullReductionService;
import com.coffee.order.service.sku.bean.UrbanFullReduction;
import com.coffee.order.service.sku.dao.UrbanFullReductionDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class UrbanFullReductionServiceImpl implements UrbanFullReductionService {
    @Resource
    private UrbanFullReductionDao urbanFullReductionDao;

    @Override
    public Page<UrbanFullReductionResult> getPager(UrbanFullReductionQuery param, PagerInfo pr) {
        Page<UrbanFullReductionResult> pager = new Page<UrbanFullReductionResult>(pr.getStart(), pr.getPageSize());
        Integer count = urbanFullReductionDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<UrbanFullReduction> list = urbanFullReductionDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, UrbanFullReductionResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(UrbanFullReductionQuery param) {
        Integer count = urbanFullReductionDao.count(param);
        return count;
    }

    @Override
    public UrbanFullReductionResult getDetail(Integer id) {
        UrbanFullReduction item = urbanFullReductionDao.findById(id);
        if (item == null) {
            return null;
        }
        UrbanFullReductionResult result = new UrbanFullReductionResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<UrbanFullReductionResult> getList(UrbanFullReductionQuery param, PagerInfo pr) {
        List<UrbanFullReduction> list = urbanFullReductionDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, UrbanFullReductionResult.class);
    }

    @Override
    public List<UrbanFullReductionResult> getList(UrbanFullReductionQuery param) {
        List<UrbanFullReduction> list = urbanFullReductionDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, UrbanFullReductionResult.class);
    }

    @Override
    public List<UrbanFullReductionResult> getList() {
        UrbanFullReductionQuery param = new UrbanFullReductionQuery();
        List<UrbanFullReduction> list = urbanFullReductionDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, UrbanFullReductionResult.class);
    }


    @Override
    public void delete(Integer id) {
        urbanFullReductionDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(UrbanFullReductionResult result) throws BusinessException {
        UrbanFullReduction item = new UrbanFullReduction();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            UrbanFullReductionQuery param = new UrbanFullReductionQuery();
            param.setCityId(result.getCityId());
            UrbanFullReduction tmp = urbanFullReductionDao.findFirst(param);
            if (tmp != null) {
                throw new BusinessException(ExceptionEnum.ERROR_1875.getCode(), ExceptionEnum.ERROR_1875.getMessage());
            }
            urbanFullReductionDao.insert(item);
        } else {
            UrbanFullReduction tmp = urbanFullReductionDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            urbanFullReductionDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, UrbanFullReductionResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, UrbanFullReductionResult> map = new HashMap<Integer, UrbanFullReductionResult>();
        List<UrbanFullReduction> list = urbanFullReductionDao.findByIds(StringUtil.setToList(idSet));
        List<UrbanFullReductionResult> resultList = DTOUtils.convertList(list, UrbanFullReductionResult.class);
        for (UrbanFullReductionResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, UrbanFullReductionResult> getItemMap() {
        Map<Integer, UrbanFullReductionResult> map = new HashMap<Integer, UrbanFullReductionResult>();
        UrbanFullReductionQuery param = new UrbanFullReductionQuery();
        List<UrbanFullReduction> list = urbanFullReductionDao.page(param, 0, Integer.MAX_VALUE);
        List<UrbanFullReductionResult> resultList = DTOUtils.convertList(list, UrbanFullReductionResult.class);
        for (UrbanFullReductionResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


}
